package com.ckd.flyingtrip.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.utils.Tools;


@SuppressLint("InflateParams")
public class GoumaiDialog extends Dialog {

    public GoumaiDialog(Context context) {
        super(context);
    }

    public GoumaiDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {

        private Context context;
        private PriorityListener listener;

        public Builder(Context context, PriorityListener listener) {
            this.context = context;
            this.listener = listener;
        }

        /**
         * 回调函数，用于在Dialog的监听事件触发后刷新Activity的UI显示
         */
        public interface PriorityListener {
            public void refreshPriorityUI(String f);
        }

        private LinearLayout weixin_iv, alipay_iv;


        @SuppressWarnings("deprecation")
        public GoumaiDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater
                    .inflate(R.layout.goumai_dialog, null);
            final GoumaiDialog dialog = new GoumaiDialog(context,
                    R.style.MyDialog);
            Window window = dialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            window.setWindowAnimations(R.style.MyDialogAnim);

            // 点击Dialog之外的区域对话框消失
            dialog.setCanceledOnTouchOutside(true);
            dialog.setContentView(layout);

            // 获取窗体信息
            WindowManager wm = ((Activity) context).getWindowManager();
            Display d = wm.getDefaultDisplay();

            WindowManager.LayoutParams p = dialog.getWindow()
                    .getAttributes();
            p.width = (int) (d.getWidth() * 1);
//            p.height = (int) (d.getHeight() * 0.25);

            dialog.getWindow().setAttributes(p);

            weixin_iv = layout.findViewById(R.id.weixin_iv);
            alipay_iv = layout.findViewById(R.id.alipay_iv);

            weixin_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(Tools.isWeixinAvilible(context)) {
                        listener.refreshPriorityUI("weixin");
                        dialog.dismiss();
                    }else {
                        Toast.makeText(context,"您未安装微信", Toast.LENGTH_LONG).show();
                    }
                }
            });

            alipay_iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(Tools.isAliPayInstalled(context)) {
                        listener.refreshPriorityUI("alipay");
                        dialog.dismiss();
                    }else {
                        Toast.makeText(context,"您未安装支付宝", Toast.LENGTH_LONG).show();
                    }
                }
            });


            return dialog;
        }
    }
}
